package com.shopnight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.entity.*;
import com.shopnight.mapper.*;
import com.shopnight.service.TakeoutGoodsCategoryService;
import com.shopnight.vo.GoodsAttributeVo;
import com.shopnight.vo.SpecificationVo;
import com.shopnight.vo.TakeoutCategoryGoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
* @author 13201
* @description 针对表【takeout_goods_category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-03-10 14:54:24
*/
@Service
public class TakeoutGoodsCategoryServiceImpl extends ServiceImpl<TakeoutGoodsCategoryMapper, TakeoutGoodsCategory>
    implements TakeoutGoodsCategoryService{

    @Autowired
    TakeoutGoodsCategoryMapper takeoutGoodsCategoryMapper;
    @Autowired
    TakeoutGoodsMapper takeoutGoodsMapper;

    @Autowired
    TakeoutGoodsSetmealMapper takeoutGoodsSetmealMapper;
    @Autowired
    TakeoutGoodsAttributeMapper takeoutGoodsAttributeMapper;
    @Autowired
    TakeoutGoodsSpecificationMapper takeoutGoodsSpecificationMapper;
    @Autowired
    TakeoutGoodsSpecificationValueMapper takeoutGoodsSpecificationValueMapper;


    @Override
    public List<TakeoutCategoryGoodsVo> getTakeoutCategoryGoods(Integer id) throws ExecutionException, InterruptedException {

        //查询商家的所有分类
        CompletableFuture<List<TakeoutGoodsCategory>> FutureOne = CompletableFuture.supplyAsync(() -> {
            List<TakeoutGoodsCategory> takeoutGoodsCategories = takeoutGoodsCategoryMapper.selectList(
                    new LambdaQueryWrapper<TakeoutGoodsCategory>()
                            .eq(TakeoutGoodsCategory::getMerchantId, id)
                            .orderByAsc(TakeoutGoodsCategory::getSort)
            );
            return takeoutGoodsCategories;
        });

        //查询商家的所有商品
        CompletableFuture<List<TakeoutGoods>> FutureTwo = CompletableFuture.supplyAsync(() -> {
            List<TakeoutGoods> takeoutGoods = takeoutGoodsMapper.selectList(
                    new LambdaQueryWrapper<TakeoutGoods>()
                            .eq(TakeoutGoods::getMerchantId, id)
            );
            return takeoutGoods;
        });

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(FutureOne, FutureTwo);
        voidCompletableFuture.get();


        List<TakeoutGoodsCategory> takeoutGoodsCategories = FutureOne.get();
        List<TakeoutGoods> takeoutGoods = FutureTwo.get();


        List<TakeoutCategoryGoodsVo> takeoutCategoryGoodsVoList = takeoutGoodsCategories.parallelStream().map(takeoutGoodsCategory -> {
            TakeoutCategoryGoodsVo takeoutCategoryGoodsVo = new TakeoutCategoryGoodsVo();
            BeanUtils.copyProperties(takeoutGoodsCategory, takeoutCategoryGoodsVo);

            //当为菜品的时候
            if (takeoutGoodsCategory.getType() == 1){
                List<TakeoutCategoryGoodsVo.TakeoutGoodsVO> collect = takeoutGoods.stream().filter(goods ->
                        goods.getCategoryId() == takeoutCategoryGoodsVo.getId()
                ).map(goods -> {
                    TakeoutCategoryGoodsVo.TakeoutGoodsVO takeoutGoodsVO = new TakeoutCategoryGoodsVo.TakeoutGoodsVO();
                    BeanUtils.copyProperties(goods, takeoutGoodsVO);
                    return takeoutGoodsVO;
                }).collect(Collectors.toList());
            //商品id集合
                List<Integer> goodsIds = collect.stream().map(item -> item.getId()).collect(Collectors.toList());
                //查询商家菜品的属性
                LambdaQueryWrapper<TakeoutGoodsAttribute> wrapper = new LambdaQueryWrapper<>();
                wrapper.in(goodsIds.size()>0,TakeoutGoodsAttribute::getGoodsId,goodsIds);
                List<TakeoutGoodsAttribute> takeoutGoodsAttributes = takeoutGoodsAttributeMapper.selectList(wrapper);
                //查询商家菜品的规格
                LambdaQueryWrapper<TakeoutGoodsSpecification> SpecificationWrapper = new LambdaQueryWrapper<>();
                SpecificationWrapper.in(TakeoutGoodsSpecification::getGoodsId,goodsIds);
                List<TakeoutGoodsSpecification> takeoutGoodsSpecifications = takeoutGoodsSpecificationMapper.selectList(SpecificationWrapper);


                collect = collect.stream().map(goodsVO -> {

                    List<GoodsAttributeVo> takeoutGoodsAttribute = takeoutGoodsAttributes.stream().filter(goodsAttribute -> {
                        return goodsVO.getId() == goodsAttribute.getGoodsId();
                    }).map(goodsAttribute->{
                        GoodsAttributeVo goodsAttributeVo = new GoodsAttributeVo();
                        BeanUtils.copyProperties(goodsAttribute,goodsAttributeVo);
                        return goodsAttributeVo;
                    }).collect(Collectors.toList());
                    //设置菜品属性
                    goodsVO.setAttributeList(takeoutGoodsAttribute);

                    List<SpecificationVo> Specification = takeoutGoodsSpecifications.stream().filter(goodsSpecifications -> {
                        return goodsVO.getId() == goodsSpecifications.getGoodsId();
                    }).map(goodsSpecifications -> {
                        SpecificationVo specificationVo = new SpecificationVo();

                        List<TakeoutGoodsSpecificationValue> takeoutGoodsSpecificationValues = takeoutGoodsSpecificationValueMapper.selectList(
                                new LambdaQueryWrapper<TakeoutGoodsSpecificationValue>()
                                        .eq(TakeoutGoodsSpecificationValue::getSpecificationId, goodsSpecifications.getId())
                        );
                        List<SpecificationVo.SpecificationValueVo> specificationValue = takeoutGoodsSpecificationValues.stream().map(item -> {
                            SpecificationVo.SpecificationValueVo specificationValueVo = new SpecificationVo.SpecificationValueVo();
                            BeanUtils.copyProperties(item, specificationValueVo);
                            return specificationValueVo;
                        }).collect(Collectors.toList());

                        specificationVo.setSpecification(goodsSpecifications.getSpecification());
                        specificationVo.setSpecificationValueVoList(specificationValue);
                        return specificationVo;
                    }).collect(Collectors.toList());

                     if (Specification.size()>0) goodsVO.setSpecification(Specification.get(0));
                    return goodsVO;
                }).collect(Collectors.toList());

                takeoutCategoryGoodsVo.setTakeoutGoodsList(collect);
            }else {
            //为套餐的时候
                LambdaQueryWrapper<TakeoutGoodsSetmeal> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TakeoutGoodsSetmeal::getCategoryId,takeoutCategoryGoodsVo.getId());
                List<TakeoutGoodsSetmeal> takeoutGoodsSetmeals = takeoutGoodsSetmealMapper.selectList(queryWrapper);
                List<TakeoutCategoryGoodsVo.TakeoutSetmealVO> collect = takeoutGoodsSetmeals.stream().map(takeoutGoodsSetmeal -> {
                    TakeoutCategoryGoodsVo.TakeoutSetmealVO takeoutSetmealVO = new TakeoutCategoryGoodsVo.TakeoutSetmealVO();
                    BeanUtils.copyProperties(takeoutGoodsSetmeal, takeoutSetmealVO);
                    return takeoutSetmealVO;
                }).collect(Collectors.toList());
                takeoutCategoryGoodsVo.setTakeoutSetmealList(collect);
            }

            return takeoutCategoryGoodsVo;
        }).collect(Collectors.toList());

        return takeoutCategoryGoodsVoList;
    }

}




